<?php

if (!isset($_SESSION)) {
    session_start();
}

class login extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    protected $configs;

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('smarty3', '', 'smarty');
        $this->load->model('pushclimb/user_model');
        $this->user_model = new user_model();
    }
    function un(){
        unset($_SESSION["push"]["user"]);
        redirect(base_url("login"));
    }
    public function index() {
        $Data=null;
        if(isset($_POST["loginEmail"]) && isset($_POST["loginPassword"])){
            $e=$_POST["loginEmail"];
            $p=$_POST["loginPassword"];
            if($_POST["loginmembership"]=="Commercial"){
                $tmp=$this->user_model->loginCommercial($e, $p);
                if(isset($tmp[0])){
                    $user=array(
                        "Email"=>$e,
                        "Name"=>$tmp[0]->CompanyName,
                        "Address"=>$tmp[0]->Address,
                        "Phone"=>$tmp[0]->Phone
                    );
                    $_SESSION["push"]["user"]=$user;
                    redirect(base_url("shop"));return;
                }else{
                    $Data["e"]="Login Fail. Please check your email and password.";
                }
            }else{
                $tmp=$this->user_model->loginPersonal($e, $p);
                if(isset($tmp[0])){
                    $user=array(
                        "Email"=>$e,
                        "Name"=>$tmp[0]->Name,
                        "Address"=>$tmp[0]->Address,
                        "Phone"=>$tmp[0]->Phone
                    );
                    $_SESSION["push"]["user"]=$user;
                    redirect(base_url("shop"));return;
                }else{
                    $Data["e"]="Login Fail. Please check your email and password.";
                }
            }
        }
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/11_login");
    }

    function regCommercial() {
        $msgs = array();
        if ((!isset($_POST["CompanyName"])) || $_POST["CompanyName"] == "") {
            $msgs[] = "CompanyName does not empty.";
        }
        if ((!isset($_POST["VATNo"])) || $_POST["VATNo"] == "") {
            $msgs[] = "VATNo does not empty.";
        }
        if ((!isset($_POST["Address"])) || $_POST["Address"] == "") {
            $msgs[] = "Official Address does not empty.";
        }
        if ((!isset($_POST["Industry"])) || $_POST["Industry"] == "") {
            $msgs[] = "Industry does not empty.";
        }
        if ((!isset($_POST["Website"])) || $_POST["Website"] == "") {
            $msgs[] = "Website does not empty.";
        }
        if ((!isset($_POST["Phone"])) || $_POST["Phone"] == "") {
            $msgs[] = "Phone does not empty.";
        }
        if ((!isset($_POST["Email"])) || $_POST["Email"] == "") {
            $msgs[] = "Email does not empty.";
        }
        if (preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["Email"]) === 0) {
            $msgs[] = "Email is not valid.";
        }
        if ((!isset($_POST["Password"])) || $_POST["Password"] == "") {
            $msgs[] = "Email does not empty.";
        }elseif(strlen($_POST["Password"])<6){
            $msgs[] = "Email does not match.";
        }
        if (count($msgs) > 0) {
            $code = -44;
            $msg = "";
            foreach ($msgs as $m) {
                $msg.="$m<br/>";
            }
        } else {
            $Params = array(
                "CompanyName" => $_POST["CompanyName"],
                "VATNo" => $_POST["VATNo"],
                "Address" => $_POST["Address"],
                "Industry" => $_POST["Industry"],
                "Website" => $_POST["Website"],
                "Phone" => $_POST["Phone"],
                "Email" => $_POST["Email"],
                "Password"=> md5($_POST["Password"])
            );
            if ($this->user_model->insertCommercial($Params)) {
                $code = 1;
                $msg = "Success. Thank you for registration.";
                $user=array(
                    "Email"=>$_POST["Email"],
                    "Name"=>$_POST["CompanyName"],
                    "Address"=>$_POST["Address"],
                    "Phone"=>$_POST["Phone"]
                );
                $_SESSION["push"]["user"]=$user;
            } else {
                $code = -1;
                $msg = "Fail. Please try again.";
            }
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }

    function regPersonal() {
        $msgs = array();
        if ((!isset($_POST["Name"])) || $_POST["Name"] == "") {
            $msgs[] = "Name does not empty.";
        }
        if ((!isset($_POST["Gender"])) || $_POST["Gender"] == "") {
            $msgs[] = "Gender does not empty.";
        }
        if ((!isset($_POST["Age"])) || $_POST["Age"] == "") {
            $msgs[] = "Age does not empty.";
        }
        if ((!isset($_POST["Level"])) || $_POST["Level"] == "") {
            $msgs[] = "Level does not empty.";
        }
        if ((!isset($_POST["Address"])) || $_POST["Address"] == "") {
            $msgs[] = "Address does not empty.";
        }
        if ((!isset($_POST["Phone"])) || $_POST["Phone"] == "") {
            $msgs[] = "Phone does not empty.";
        }
        if ((!isset($_POST["Email"])) || $_POST["Email"] == "") {
            $msgs[] = "Email does not empty.";
        }
        if (preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["Email"]) === 0) {
            $msgs[] = "Email is not valid.";
        }
        if ((!isset($_POST["Password"])) || $_POST["Password"] == "") {
            $msgs[] = "Email does not empty.";
        }elseif(strlen($_POST["Password"])<6){
            $msgs[] = "Email does not match.";
        }
        if (count($msgs) > 0) {
            $code = -44;
            $msg = "";
            foreach ($msgs as $m) {
                $msg.="$m<br/>";
            }
        } else {
            
            $Params = array(
                "Name" => $_POST["Name"],
                "Gender" => $_POST["Gender"],
                "Address" => $_POST["Address"],
                "Age" => $_POST["Age"],
                "Level" => $_POST["Level"],
                "Phone" => $_POST["Phone"],
                "Email" => $_POST["Email"],
                "Password"=> md5($_POST["Password"])
            );
            //print_r($Params);return;
            if ($this->user_model->insertPersonal($Params)) {
                $code = 1;
                $msg = "Success. Thank you for registration.";
                $user=array(
                    "Email"=>$_POST["Email"],
                    "Name"=>$_POST["Name"],
                    "Address"=>$_POST["Address"],
                    "Phone"=>$_POST["Phone"]
                );
                $_SESSION["push"]["user"]=$user;
            } else {
                $code = -1;
                $msg = "Fail. Please try again.";
            }
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }

}

